package com.jungle.sp.daily.sms.firebase;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;

public class FirebasePhoneAuth {
    public static final String key = "-----BEGIN PRIVATE KEY-----\\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQChrSDcKKRfqoU7\\nNh+TQ1xBr6S24vOaGkFutMPhrdU3Ciu9/D7y0uPqFIn5oo6VgXUO1tFUi+7ZvSfA\\nkod4iRdfEeTNmc/WQZe/8bhT1P7D0nfoW7tGrrVhqMsyQ0Ct5TjLseLVRaYG8kYI\\nr+wmbUgO+zYQDsv2qWj+vNemxeMxDdOd+7faqGFc6AMK6ZEQTsL6f4CW6MJGzsq/\\nbC7F8g8Y277cxRDkqjzllZvh6k1m4OB8HEFMW6vO0OZ1/UASn5sCbbFBvvzBQXRe\\nZDjOgi3tex0Vz6wcBrPhOaInxTFGOlotp4Fanv7c2tVB4Fi3XI112tSVZYQZAwF+\\nKuDscqSbAgMBAAECggEACUQpzCR82J9717BwRS7zqdoeHSigdfZyRfI7VdhyOMou\\n8Up0oH3gzTOUfYfJF0gzrtWLatvtpRzNG0w//R29Bq25COWmFALEu/Nn2/orCIss\\nDy0OwewqfiiVTidXnNU0mPbJku1VKN8YOiWRvy69QLU6Mde/Qz8OGFI3eUSskveU\\numGeiuu7a0dlaLJ30cJtazPqWmyk8GESPvhmx9nMdILDWnXbIzq6Iidw18jdEXDF\\nX9VKWLSy59oqJvOdIQQYp//OcsMJ2h6Ir6NtpHNL3xLwO+7b4NUXKxSczS6+mE5o\\n0s9wUtAjWBLLpOOro7F5BQ3UVvGTAvMcbPl0eWW1GQKBgQDUl8x32WtAFsQ585hz\\nEbqTTu0+X+pEAgVMWyqqGZdN9ZUGS55R+4fjotXE6qDFzWAHEwugKAxPiJOC80mg\\nvm98gzhpa1eFhUdXgCuT93tCT6bupcNw7vjI7sXEA0eB0X/pFfQKsjrDmqJCfmuB\\nYwZ9/Ai1QE5Cgo12oC6/F51RCQKBgQDCr+ww6s794NcwjgQ3BpfJSCI7PWmEQNsr\\n2oRN02ED4q9WDpkrrflOZe9PQTt0ViqLSWssBLeeNeC+XTLQ6fE8GoMjsxbx2FHu\\njn7NIKkvljsv+txpLbzwTutT/APuViNPvRgnHZYB+MCPmrw+EgaFWO9kA3tJmCHH\\nEISu8bUFgwKBgEaqDnTn+BZdeuwoZEwbYIEGgyVwjFX+tfQjeZMA3qIzN03YZM+r\\nZAfuYFAMDVSyZ3Dyi7K4nR7yrtWgOKENq9EfbOjdnUHsg0I6gEG2yWnJQBujTcuE\\nI33moCX/UQ19rZ60hWHozE+oXrbrfqCLYeUOmXZl4Z2J96uuotsx5kDBAoGAeK0x\\nW3UAzy+CU4+EH1OP3G1BArVjZV4KPNmbLt56gT0O7+nmmcOiCC15NAg8QTVz85iT\\nvNpDsua0o2sDp6mLJXVNmmBVNZ2f054yDRZUqLGXiHsYCn4WYmKYzhslmKN+Cdwt\\nT3mwVRKK0bjZPCRSNG/XG4ho8EpDja+4fvRF/98CgYBwIM8QsK6smP5/tNegHupW\\nznGELRSopjp4EaVYcGjdNH8A4wtpPrSXSSX61yOsiXYJB8MWAvhSOfZY+qeGSvsp\\nND6FlZEacobscQD+KA1v1jRkT/SeOnhJtb+WicLHfa4x5idtvyDsWftNH3cnbZH4\\nbZLPm5t4SIZtiDNuH1JVMg==\\n-----END PRIVATE KEY-----\\n";

    private static final String FIREBASE_AUTH_URL = "https://identitytoolkit.googleapis.com/v1/accounts:sendVerificationCode?key="+key;

    public static void sendVerificationCode(String phoneNumber) throws Exception {
        // 创建代理对象，指定代理服务器的地址和端口
        String proxyHost = "127.0.0.1";  // 替换为代理服务器的主机名或 IP 地址
        int proxyPort = 10809;                   // 替换为代理服务器的端口号
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));

        // 创建连接对象，指定使用代理
        URL url = new URL(FIREBASE_AUTH_URL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);  // 使用代理
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "application/json");
        connection.setDoOutput(true);

        // 构建请求体
        String requestBody = "{"
                + "\"phoneNumber\": \"" + phoneNumber + "\","
                + "\"recaptchaToken\": \"RECAPTCHA_TOKEN\""
                + "}";

        // 发送请求
        try (OutputStream os = connection.getOutputStream()) {
            byte[] input = requestBody.getBytes("utf-8");
            os.write(input, 0, input.length);
        }

        // 读取响应
        try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"))) {
            StringBuilder response = new StringBuilder();
            String responseLine;
            while ((responseLine = br.readLine()) != null) {
                response.append(responseLine.trim());
            }
            System.out.println(response.toString());
        }
    }

    public static void main(String[] args) throws Exception {
        sendVerificationCode("+8618949555420");
    }
}
